<?php

/*
 * 商家模型
 * @author wyb
 */

namespace Qwadmin\Model;

class GoodsApiModel extends ApiModel {

    //自动组合入库数据 微三云
    public function start($indata) {
        set_time_limit(0);
        $goods = new GoodsModel();

        do {
            $op = M('goods_op')->where("rsp is null or rsp=''")->limit(30)->select();
            var_dump($op);
            
            foreach ($op as $k => $p) {
                if (in_array($p['op'], array(ADD_NEW, UPDATE_GOODS))) {
                    $value = $goods->where(array("id" => $p['goods_id']))->find();
                    $goods->get_goods_img($p['goods_id']);
                    if (!$goods->checkgoods($value)) {
                        $where = array('goods_id' => $p['goods_id'], 'site_id' => $p['site_id'], 'store_id' => $p['store_id']);
                        M('goods_op')->where($where)->data(array('rsp' => 'error', 'log' => '商品在添加、更新时商品检查不通过', 'optime' => time()))->save();
                        continue;
                    }
                }

                $re = array();
                $api = M('shop_api')->where(array(array('shop_id' => $p['site_id']), array('flag' => $p['op'])))->find();
                if ($api) {
                    $obj = new $api['execute_obj']($api['id']);
                    $re = $obj->$api['method']($p['goods_id'], $p['site_id'], $p['store_id']);
                    //echo "add new goods execute.mothed:<br>";
                    echo $api['execute_obj'] . "->" . $api['method'] . "(" . $p['goods_id'] . "," . $p['site_id'] . "," . $p['store_id'] . ")<br>";
                } else {
                    $re = array(
                        "rsp" => 'error',
                        "log" => '接口方法不存在',
                        "optime" => time(),
                    );
                }

                if (!$re) {
                    $where = array('goods_id' => $p['goods_id'], 'site_id' => $p['site_id'], 'store_id' => $p['store_id']);
                    M('goods_op')->where($where)->data(array('rsp' => 'error', 'log' => '可能因为接口原因产生的错误', 'optime' => time()))->save();
                } else {
                    ////更新添加状态
                    $p['rsp'] = $re['rsp'];
                    $p['log'] = $re['log'];
                    $p['optime'] = $re['optime'];
                    M('goods_op')->where('id=' . $p['id'])->save($p);
                }
            }
        } while (count($op) == 30);
        //$page=new  \Qwadmin\Model
    }

}
